package com.central.apps.handler;

import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import com.central.common.auth.details.LoginAppUser;
import com.central.common.util.SysUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class AssetchangeHandler extends LvBaseHandler {

    @Autowired private HandlerService handlerService;

    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {
        if(flowNode.getId().equals("yyg")){
            //获取当前资产变更信息
            String getInfos ="select * from bl_assetchange where id ="+id;
            List<Map<String, Object>> getInfosbySql = handlerService.findBySql(getInfos);
            String changenum = MapUtils.getString(getInfosbySql.get(0),"changenum");
            String assetnum = MapUtils.getString(getInfosbySql.get(0),"assetnum");
            String assetstatus =MapUtils.getString(getInfosbySql.get(0),"assetstatus2");
            String deptfullname = MapUtils.getString(getInfosbySql.get(0),"deptfullname2");
            String assetlocation = MapUtils.getString(getInfosbySql.get(0),"assetlocation2");
            String locationdesc = MapUtils.getString(getInfosbySql.get(0),"locationdesc2");
            String installsite = MapUtils.getString(getInfosbySql.get(0),"installsite2");
            String equipmentclassify = MapUtils.getString(getInfosbySql.get(0),"equipmentclassify2");
            String classifydescribe = MapUtils.getString(getInfosbySql.get(0),"classifydescribe2");
            String classification = MapUtils.getString(getInfosbySql.get(0),"classification2");
            String parent = MapUtils.getString(getInfosbySql.get(0),"parent2");
            String parentdesc = MapUtils.getString(getInfosbySql.get(0),"parentdesc2");
            String istzsb = MapUtils.getString(getInfosbySql.get(0),"istzsb2");
            String usedepartment = MapUtils.getString(getInfosbySql.get(0),"usedepartment2");
            String Responsible = MapUtils.getString(getInfosbySql.get(0),"Responsible2");
            String productnum = MapUtils.getString(getInfosbySql.get(0),"productnum2");
            String drawingnum = MapUtils.getString(getInfosbySql.get(0),"drawingnum2");
            String specification = MapUtils.getString(getInfosbySql.get(0),"specification2");
            String originalvalue = MapUtils.getString(getInfosbySql.get(0),"originalvalue2");
            String purchaseprice = MapUtils.getString(getInfosbySql.get(0),"purchaseprice2");
            String manufacturer = MapUtils.getString(getInfosbySql.get(0),"manufacturer2");
            String supplier = MapUtils.getString(getInfosbySql.get(0),"supplier2");
            String manufactorperson = MapUtils.getString(getInfosbySql.get(0),"manufactorperson2");
            String manufactorphone = MapUtils.getString(getInfosbySql.get(0),"manufactorphone2");
            String useryear = (MapUtils.getString(getInfosbySql.get(0),"useryear2"));
            String deliverytime = MapUtils.getString(getInfosbySql.get(0),"deliverytime2");
            String installtime = MapUtils.getString(getInfosbySql.get(0),"installtime2");
            String productiontime = MapUtils.getString(getInfosbySql.get(0),"productiontime2");
            //更新到assetnum对应资产台账表 bl_asset
            //获取当前登录人用户名称
            LoginAppUser user = SysUserUtil.getLoginAppUser();
            String updateuserdesc =user.getNickname();
            String updSql ="update bl_asset set "
                            +"classification ='"+classification+"', status ='"+assetstatus+"', "
                            +"assetlocation ='"+assetlocation+"', locationdesc ='"+locationdesc+"', "
                            +"equipmentclassify ='"+equipmentclassify+"', classifydescribe ='"+classifydescribe+"', "
                            +"parent ='"+parent+"', parentdesc ='"+parentdesc+"', "
                            +"installsite ='"+installsite+"', deptfullname ='"+deptfullname+"', "
                            +"usedepartment ='"+usedepartment+"', Responsible ='"+Responsible+"', "
                            +"productnum ='"+productnum+"', istzsb ='"+istzsb+"', "
                            +"specification ='"+specification+"', drawingnum ='"+drawingnum+"', "
                            +"originalvalue ='"+originalvalue+"', purchaseprice ='"+purchaseprice+"', "
                            +"manufacturer ='"+manufacturer+"', supplier ='"+supplier+"', "
                            +"manufactorperson ='"+manufactorperson+"', manufactorphone ='"+manufactorphone+"', "
                            +"deliverytime ='"+deliverytime+"', installtime ='"+installtime+"', "
                            +"productiontime ='"+productiontime+"', useryear ='"+useryear+"', "
                            +"updateuserdesc ='"+updateuserdesc+"', updatetime =now() "
                            +"where assetnum ='"+assetnum+"';";
            if(updSql.indexOf("null") !=-1){
                updSql=updSql.replaceAll("'null'","null");
            }
            handlerService.updateBySql(updSql);

        }
    }
}
